package net.alche.al.util.far;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import net.alche.al.util.AC;
import net.alche.al.util.Far;
import net.alche.al.util.M;
import net.alche.al.util.far.pojo.ResultPack;
import net.alche.sdd.pojo.ApiInfo;
import net.ext.cache.CaffeineCache;

// FaBaseStore
@SuppressWarnings("all")
public class FaBaseStore {
  Map<String, CaffeineCache> cafMap = new HashMap();
  public Map<String, String> serConf = new HashMap<>();
  public List<ApiInfo> apiLs = null;

  public FaBaseStore() {
  }

  public List<ApiInfo> getApiLs() {
    return apiLs;
  }

  CaffeineCache getCache(String type) {
    CaffeineCache caf = cafMap.get(type);
    if (caf == null) {
      caf = new CaffeineCache();
      caf.init();
      cafMap.put(type, caf);
    }
    return caf;
  }

  // 设置访问过期数据缓存: 3天有效期，最大100万条
  public void setExpire(String type, String key, Object obj) {
    String ser = "main";
    boolean isBridge = false;
    if (M.with(type, ":")) {
      List<String> sp = M.splitHalf(type, ":");
      type = sp.get(1);
      ser = sp.get(0);
      isBridge = true;
    }
    if (isBridge) {
      httpBridgeCommit(ser, type, obj);
      String data = httpBridgeData(ser, type, key);
      if (M.noEmp(data)) {
        Object obb = M.toJsonObj(data, obj.getClass());
        CaffeineCache caf = getCache(type);
        caf.put(key, obb);
      }
    } else {
      CaffeineCache caf = getCache(type);
      caf.put(key, obj);
    }
  }

  // 读取数据
  public <T> T getExpire(String type, String key, Class cls) {
    String ser = "main";
    boolean isBridge = false;
    if (M.with(type, ":")) {
      List<String> sp = M.splitHalf(type, ":");
      type = sp.get(1);
      ser = sp.get(0);
      isBridge = true;
    }
    CaffeineCache caf = getCache(type);
    Object obj = caf.get(key);
    if (obj == null && isBridge) {
      String data = httpBridgeData(ser, type, key);
      if (M.noEmp(data)) {
        Object obb = M.toJsonObj(data, cls);
        obj = obb;
        setExpire(type, key, obj);
      }
    }
    return (T) obj;

  }

  // 读取数据
  public List getExpireLs(String type, String key, Class cls) {
    String ser = "main";
    boolean isBridge = false;
    if (M.with(type, ":")) {
      List<String> sp = M.splitHalf(type, ":");
      type = sp.get(1);
      ser = sp.get(0);
      isBridge = true;
    }
    CaffeineCache caf = getCache(type);
    Object obj = caf.get(key);
    if (obj == null && isBridge) {
      String data = httpBridgeData(ser, type, key);
      if (M.noEmp(data)) {
        Object obb = M.toJsonObj_list(data, cls);
        obj = obb;
        setExpire(type, key, obj);
      }
    }
    return (List) obj;
  }

  public String httpBridgeData(String ser, String type, String key) {
    String result = Far.http.getSync(
        "http://" + M.s(serConf, ser + "_ip") + ":" + M.s(serConf, ser + "_port") + "/web/bridge/store/" + type + "/"
            + key + "?token=" + M.s(serConf, ser + "_token"),
        null);
    if (M.noEmp(result)) {
      JsonObject rp = M.toJsonObj(result, JsonObject.class);
      int state = rp.asMap().get("state").getAsInt();
      if (state == 1) {
        return rp.asMap().get("data").toString();
      }
    }
    return null;
  }

  public String httpBridgeCommit(String ser, String type, Object obj) {
    String result = Far.http.postSync(
        "http://" + M.s(serConf, ser + "_ip") + ":" + M.s(serConf, ser + "_port") + "/web/bridge/store/" + type
            + "?token=" + M.s(serConf, ser + "_token"),
        obj);
    if (M.noEmp(result)) {
      JsonObject rp = M.toJsonObj(result, JsonObject.class);
      int state = rp.asMap().get("state").getAsInt();
      if (state == 1) {
        return rp.asMap().get("data").toString();
      }
    }
    return null;
  }
}